package com.sgb.kjwl.view.ui.activity.carrierDemand;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import com.google.android.material.tabs.TabLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;

import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarrierDemandBinding;
import com.sgb.kjwl.di.carrierDemand.CarrierDemandModule;
import com.sgb.kjwl.di.carrierDemand.DaggerCarrierDemandComponent;
import com.sgb.kjwl.model.entity.transport.CarrierListResultEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.carrierDemand.CarrierDemandListAdapter;
import com.sgb.kjwl.view.widget.CarsInfoMorePopWindow3;
import com.sgb.kjwl.view.widget.GoodsMessagePopWindow3;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.viewmodel.carrierDemand.CarrierDemandAtvModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 承运需求 列表
 */

public class CarrierDemandActivity extends AppActivity {
    private int LOCATION_TYPE = -1;//0发货地，1目的地
    @Inject
    CarrierDemandAtvModel viewModel;

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private CarsInfoMorePopWindow3 carsInfoMorePopWindow;
    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private SendGoodsLocationPopWindow3 destinationPopWindow;

    ActivityCarrierDemandBinding binding;

    private List<GoodsInfoTypeEntity3> carsTypeData;
    private List<GoodsInfoTypeEntity3> carsLengthData;

    private CarrierDemandListAdapter carrierDemandListAdapter; //接口完成 替换数据
    private int page = 1;
    private boolean isLoadMore = false;

    private GoodsMessagePopWindow3 messagePopWindow;
    private String carTypeNo = "";
    private String carLengthNo = "";
    private int typePosition = -1;
    private int lengthPosition = -1;
    private GoodsTypePopWindow3 carsTypePopWindow;
    private GoodsTypePopWindow3 carsLengthPopWindow;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerCarrierDemandComponent.builder().appComponent(appComponent).carrierDemandModule(new CarrierDemandModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_carrier_demand);
        binding.setViewModel(viewModel);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("承运需求");
        binding.sendLocationLayout.setOnClickListener(this);
        binding.purposeLocationLayout.setOnClickListener(this);
        binding.carsMoreLayout.setOnClickListener(this);
        binding.messageNumLayout.setOnClickListener(this);
        binding.typeMore.setOnClickListener(this);
        binding.lengthMore.setOnClickListener(this);

        if (carrierDemandListAdapter == null) {
            carrierDemandListAdapter = new CarrierDemandListAdapter(getActivity(), null);
        }
        binding.carsList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.carsList.setAdapter(carrierDemandListAdapter);
        carrierDemandListAdapter.setOnItemClick(new AdapterOnItemClick<CarrierListResultEntity>() {
            @Override
            public void onItemClick(CarrierListResultEntity carrierListResultEntity, int position) {
                LogUtil.v("carrierInfo=", carrierListResultEntity.getNo() + "");
//                viewModel.getH5Url(carrierListResultEntity.getId());
            }
        });

        binding.carsList.setLoadingMoreEnabled(true);
        binding.carsList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isLoadMore = false;
                viewModel.getCarrierInfoList(page);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                viewModel.getCarrierInfoList(page);
            }
        });

        //类型
        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(null!=carsTypeData && carsTypeData.size()>0){
                    carTypeNo = carsTypeData.get(tab.getPosition()).getNo();
                    typePosition = tab.getPosition();
                    viewModel.carType.set(carsTypeData.get(tab.getPosition()));
                    getData();
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

        //类型
        carsTypePopWindow = new GoodsTypePopWindow3(getActivity());
        carsTypePopWindow.setBgView(binding.grayLayout);
        carsTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                typePosition = (int) key2;
                carTypeNo = entity.getNo();
                binding.tabLayoutType.setScrollPosition(typePosition,0,true);
                viewModel.carType.set(entity);
                getData();
            }
        });

        //车长
        binding.tabLayoutLength.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(null!=carsLengthData && carsLengthData.size()>0){
                    carLengthNo = carsLengthData.get(tab.getPosition()).getNo();
                    lengthPosition = tab.getPosition();
                    viewModel.carLength.set(carsLengthData.get(tab.getPosition()));
                    getData();
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

        //车长
        carsLengthPopWindow = new GoodsTypePopWindow3(getActivity());
        carsLengthPopWindow.setBgView(binding.grayLayout);
        carsLengthPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                lengthPosition = (int) key2;
                carLengthNo = entity.getNo();
                binding.tabLayoutLength.setScrollPosition(lengthPosition,0,true);
                viewModel.carLength.set(entity);
                getData();
            }
        });

        //发货省份popWindow
        sendGoodsLocationPopWindow3 = new SendGoodsLocationPopWindow3(getActivity());
        sendGoodsLocationPopWindow3.setBgView(binding.grayLayout);
        sendGoodsLocationPopWindow3.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvSend.setText(entity.getCityName());
                viewModel.sendLocation.set(entity);
                page = 1;
                isLoadMore = false;
                viewModel.getCarrierInfoList(page);
                LogUtil.v("goodsInfo", "发货物地址=" + entity.getProvinceName() + "==" + entity.getCityName() + "==" + entity.getAreaName());
            }
        });
        sendGoodsLocationPopWindow3.setOnDismissListener(() -> binding.sendGoodsIv.setSelected(false));

        //收货省份popWindow
        destinationPopWindow = new SendGoodsLocationPopWindow3(getActivity());
        destinationPopWindow.setBgView(binding.grayLayout);
        destinationPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvDestination.setText(entity.getCityName());
                viewModel.purposeLocation.set(entity);

                page = 1;
                isLoadMore = false;
                viewModel.getCarrierInfoList(page);
                LogUtil.v("goodsInfo", "货物目的地址=" + entity.getProvinceName() + "==" + entity.getCityName() + "==" + entity.getAreaName());
            }
        });
        destinationPopWindow.setOnDismissListener(() -> binding.purposeGoodsIv.setSelected(false));


        //更多
        carsInfoMorePopWindow = new CarsInfoMorePopWindow3(getActivity());
        carsInfoMorePopWindow.setBgView(binding.grayLayout);
        carsInfoMorePopWindow.setOnDismissListener(() -> binding.goodsMoreIv.setSelected(false));
        carsInfoMorePopWindow.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
            @Override
            public void result(Object result) {
                GoodsSourceFilterEntity filterEntity = (GoodsSourceFilterEntity) result;
                viewModel.filterEntity.set(filterEntity);
                page = 1;
                isLoadMore = false;
                viewModel.getCarrierInfoList(page);
            }
        });

        //消息
        messagePopWindow = new GoodsMessagePopWindow3(this);
        messagePopWindow.setBgView(binding.bgPopGrayLayout);
        loadData();
    }

    private void getData() {
        viewModel.getCarrierInfoList(page);
    }

    //列表
    public void getCarrierInfoData(List<CarrierListResultEntity> list) {
        binding.carsList.refreshComplete();
        if (isLoadMore) {
            binding.carsList.loadMoreComplete();
            if (list.size() > 0) {
                carrierDemandListAdapter.addDatas(list);
            } else {
                binding.carsList.setLoadingMoreEnabled(false);
            }
            binding.carsList.setVisibility(View.VISIBLE);
        } else {
            binding.carsList.refreshComplete();
            if (list.size() > 0) {
                binding.carsList.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                carrierDemandListAdapter.setDatas(list);
                binding.carsList.setLoadingMoreEnabled(true);
            } else {
                carrierDemandListAdapter.setDatas(null);
                binding.carsList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CarrierDemandActivity.class);
        context.startActivity(intent);
    }

    private void showSendPopWindow() {
        sendGoodsLocationPopWindow3.showPopwindow(binding.sendLocationLayout, 0, 0);
        sendGoodsLocationPopWindow3.setData(addressEntityList);
    }

    private void showDestinationPopWindow() {
        destinationPopWindow.showPopwindow(binding.sendLocationLayout, 0, 0);
        destinationPopWindow.setData(addressEntityList);
    }

    private void showMoreFilterPopWindow() {
        carsInfoMorePopWindow.showPopwindowRight(binding.carsMoreLayout);
        carsInfoMorePopWindow.setData(carsTypeData, carsLengthData);
    }

    private void loadData() {
        viewModel.getCarsLength();
        viewModel.getCarsType();

        viewModel.getCarrierInfoList(page);
    }

    private void showCarsTypePopWindow() {
        carsTypePopWindow.showPopwindow(binding.tabLayoutType, 0, 0);
        carsTypePopWindow.setData(carsTypeData,typePosition);
    }

    private void showCarsLengthPopWindow() {
        carsLengthPopWindow.showPopwindow(binding.tabLayoutLength, 0, 0);
        carsLengthPopWindow.setData(carsLengthData,lengthPosition);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.cars_more_layout:
                setOptionStatus(4);
                if (null != carsTypeData && carsTypeData.size() > 0 && null != carsLengthData && carsLengthData.size() > 0) {
                    showMoreFilterPopWindow();
                } else {
                    viewModel.getGoodsTypeData();
                }
                break;

            case R.id.type_more:
                if (null != carsTypeData && carsTypeData.size() > 0) {
                    showCarsTypePopWindow();
                }
                break;
            case R.id.length_more:
                if (null != carsLengthData && carsLengthData.size() > 0) {
                    showCarsLengthPopWindow();
                }
                break;
            case R.id.send_location_layout:
                LOCATION_TYPE = 0;
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showSendPopWindow();
                } else {
                    viewModel.getAddressModel();
                }
                setOptionStatus(1);
                break;
            case R.id.purpose_location_layout:
                LOCATION_TYPE = 1;
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showDestinationPopWindow();
                } else {
                    viewModel.getAddressModel();
                }
                setOptionStatus(2);
                break;

            case R.id.message_num_layout:
                //消息
                messagePopWindow.showPopwindowRight(binding.messageNumLayout);
                break;
        }
    }

    private void setOptionStatus(int index) {
        switch (index) {
            case 1:
                binding.tvSend.setSelected(true);
                binding.sendGoodsIv.setSelected(true);
                break;
            case 2:
                binding.tvDestination.setSelected(true);
                binding.purposeGoodsIv.setSelected(true);
                break;
            case 4:
                binding.tvMore.setSelected(true);
                binding.goodsMoreIv.setSelected(true);
                break;
        }
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        Log.v("public==source", datas.size() + "");
        initJsonData();
    }

    private void initJsonData() {

        for (int i = 0; i < addressEntityList.size(); i++) {

            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }

        if (LOCATION_TYPE == 1) {
            showDestinationPopWindow();
        } else if (LOCATION_TYPE == 0) {
            showSendPopWindow();
        }
    }

    //获取车型
    public void getCarsTypeData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsTypeData = list;
            for(int i=0;i<list.size();i++){
                binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(list.get(i).getName()));
            }

        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }

    //获取车长
    public void getCarsLengthData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsLengthData = list;
            for(int i=0;i<list.size();i++){
                binding.tabLayoutLength.addTab(binding.tabLayoutLength.newTab().setText(list.get(i).getName()));
            }

        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }


}
